package com.bk.controller;

import com.bk.interceptor.AuthInterceptor;
import com.cjw.model.DBMap;
import com.jfinal.aop.Clear;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.StrKit;
import com.moomi.common.model.BkShop;
import com.moomi.common.model.BkUser;
import com.moomi.common.model.BkUserShop;
import com.utils.AuthUtils;

public class BKUserController extends BKController {
	// BKAuthUtils authUtils = new BKAuthUtils(this);

	@Clear(AuthInterceptor.class)
	public void index() {
		render("login.html");
	}

	@Clear
	public void auth() {
		// authUtils.auth();
		String username = getPara("username");
		String password = getPara("password");
		if (StrKit.isBlank(username) || StrKit.isBlank(password)) {
			renderFailure();
			return;
		}
		try {
			DBMap map = new DBMap();
			map.put("email", username);
			map.put("password", password);
			BkUser user = BkUser.dao.searchFirst(map);
			if (user != null) {
				String token = AuthUtils.login(user.getId() + "", this);
				// LogKit.info("login suc " + token);
				// LogKit.info(AuthUtils.verify(this) + "");
				// LogKit.info(AuthUtils.getTokenUid(this) + "");
				redirect("/bk/item");
				renderHref("/bk/item");
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		LogKit.info("login fail");
		redirect("/bk/user");
		// renderFailure();
		renderMsg("账号/密码错误");
	}

	@Clear(AuthInterceptor.class)
	public void logout() {
		// authUtils.logout();
		AuthUtils.logout(this);
		redirect("/bk/user");
	}

	@Clear
	public void add() {
		BkUser user = new BkUser();
		user.setPassword("123");
		user.setEmail("123@123");
		user.save();

		BkShop shop = new BkShop();
		shop.setName("新的店铺");
		shop.save();

		BkUserShop userShop = new BkUserShop();
		userShop.setUserId(user.getId());
		userShop.setShopId(shop.getId());
		userShop.save();
	}
}
